package com.zprmb.udzt;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.zprmb.udzt.util.Constant;

/**
 * 邀请web
 * @author shenshao
 *
 */
public class InvestWeb extends BaseActivity{
	private WebView webView;
	private ImageView img_back;
	private String ref="";
	private RelativeLayout rl_back;
	 String aa="111";
    @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.investweb);
        BbtApplication.getInstance().addActvity(this);
        ref=getShare().getString("usercode", "");
        img_back=(ImageView)findViewById(R.id.img_back);
        rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		rl_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        img_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        webView = (WebView) findViewById(R.id.webView);

        webView.setVerticalScrollbarOverlay(true);
        //设置WebView支持JavaScript
        webView.getSettings().setJavaScriptEnabled(true);
        String ua = webView.getSettings().getUserAgentString();
        webView.getSettings().setUserAgentString(ua+"; "+"rmbbox_android");
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setSupportZoom(true); 
        webView.getSettings().setDomStorageEnabled(true);   
		String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();  
		webView.getSettings().setAppCachePath(appCachePath);  
		webView.getSettings().setAllowFileAccess(true);  
		webView.getSettings().setAppCacheEnabled(true); 
        String url = Constant.URL+"/h5/invite?app=true";
        webView.setWebViewClient(new WebViewClient(){
			public boolean shouldOverrideUrlLoading(WebView view, String url){
				if(url.indexOf("http://")==0||url.indexOf("https://")==0){
					
					return true;
				}
//				else if(url.indexOf("tel")==0){
//					view.loadUrl(url,extraHeaders);
//					return true;
//				}
//				view.loadUrl(Constant.URL_NEW+url,extraHeaders);	
				//view.loadUrl("http://docs.google.com/gview?embedded=true&url=http://www.navicat.com.cn/manual/pdf_manual/cn/navicat/rv_manual/rviewer_cs.pdf");
				return true;
			}
//			public void onReceivedSslError(WebView view,SslErrorHandler handler, SslError error) {
//				//super.onReceivedSslError(view, handler, error);
//				handler.proceed();
//			}
		});
        
        webView.addJavascriptInterface(new JsInterface(this), "AndroidWebView");
        webView.getSettings().setDefaultTextEncodingName("utf-8"); 
//        webView.loadUrl("file:///android_asset/Untitled1.html");
        webView.loadUrl(url);
        
       
        
        webView.setWebChromeClient(new WebChromeClient(){
        	@Override
        	public boolean onJsAlert(WebView view, String url, String message,
        			JsResult result) {
        		// TODO Auto-generated method stub
        		return super.onJsAlert(view, url, message, result);
        	}
        	@Override
	          public void onProgressChanged(WebView view, int newProgress) {
	              if (newProgress == 100) {
	                  
	              }
	              super.onProgressChanged(view, newProgress);
	          }
        });
        //在js中调用本地java方法

        //添加客户端支持
        
    }
    public void a(View view){
       
             
          
                webView.loadUrl("javascript:testAndroid('" + getToken() + "','" + getUserId() + "')"); 
            
        
    }
	@Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	BbtApplication.getInstance().removeActvity(this);
    }
    private class JsInterface {
        private Context mContext;

        public JsInterface(Context context) {
            this.mContext = context;
        }

        //在js中调用window.AndroidWebView.showInfoFromJs(name)，便会触发此方法。
        @JavascriptInterface
        public void showInfoFromJs(String name) {
          
//        	initshare();
        }
        public void sendInfoToJs(View view) {
            String msg = getToken();
           Log.e("web", "调用");
            //调用js中的函数：showInfoFromJava(msg)
            webView.loadUrl("javascript:testAndroid()");
        }
    }
//    private void initshare(){
//		VolleryUtil volleryUtil=new VolleryUtil() {
//			
//			@Override
//			public void onUpDate(JSONObject jsonObject) {
//				// TODO Auto-generated method stub
//				Log.e("invate", jsonObject.toString());
//				if(jsonObject.optInt("success")==1){
//					JSONObject json=jsonObject.optJSONObject("data");
//					Intent intent = new Intent(InvestWeb.this,WXEntryActivity.class);
//					if(ref.equals("")){
//						intent.putExtra("sharelink", json.optString("linkUrl"));
//					}else{
//						intent.putExtra("sharelink", json.optString("linkUrl")+"?ref="+ref);
//					}
//					intent.putExtra("description", json.optString("description"));
//					intent.putExtra("title", json.optString("title"));
////					intent.putExtra("imgurl", json.optString("imageUrl"));
//					startActivity(intent);
//				}else{
//				 	
//				}
//			}
//			
//			@Override
//			public void onError() {
//				// TODO Auto-generated method stub
//				
//			}
//		};
//		volleryUtil.setRequestMethod(0);
//		volleryUtil.setShow(true);
//		volleryUtil.execute(InvestWeb.this, Constant.URL+"/api/shareProductContent/inviteFriend/inviteFriend", "");
//	}
    
}
